// packageCommunity/pages/success/index.js
import{comments} from "../../../js/modules/mall.js" 
import BasePage from '../../../js/common/base-page' 
import { pagesInfo} from '../../../js/router';
import { api,util } from '../../../js/util';
var app = getApp(); 
BasePage({

  /**
   * 页面的初始数据
   */
  data: {
    postDetail:{},//帖子详情
    imgDomain:api.imgDomain,
    share:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDetail(options.id); 
  },
  bindBack:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.hideHomeButton();
  },
  getDetail:function(postId){
    var that = this;
    var data = {Id:postId};
    comments.getPostDetail(data,function(content){
      that.setData({
        share:content.Success
      })
      if(content.Success){
        var datas = content.Content;
        if(datas.ImageList[0].indexOf(',') === -1){
          datas.ImageList = datas.ImageList; 
        }else{
          datas.ImageList = datas.ImageList[0].split(',');
        }
        datas.Content = datas.Content.replace(/<[^>]+>/g, "").replace(/()+/ig, "\n")
        that.setData({
          postDetail:data 
        })
      }else{
        // util.tips(content.Message);
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this;
    let {imgDomain,postDetail} = that.data;
    let path = pagesInfo.postdetail.url + `?id=${postDetail.Id}`;
    if (res.from === 'button') {
      // console.log(res.target)
    }
    
    // todo: 图片调为5:4
    return {
      title: postDetail.Title?postDetail.Title:postDetail.Content,
      imageUrl: imgDomain+postDetail.ImageList[0],
      path: path
    }
  }
})